package io.lkt.task.util;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.format.DateTimeFormat;

import java.util.Date;

public class DateUtil {

    /**
     * 获取当前时间
     * @return
     */
    public static Date getCurDate() {
        return new Date();
    }

    /**
     * 日期格式化
     */
    public static String getFormatDate(String format, Date date) {

        DateTime dateTime = DateTime.now();
        if (date != null) {
            dateTime = new DateTime(date);
        }

        if (StringUtils.isBlank(format)) {
            format = "yyyy-MM-dd HH:mm:ss";
        }

        return dateTime.toString(format);
    }

    /**
     * 日期格式化
     */
    public static String getFormatDate(String format, DateTime date) {

        DateTime dateTime = date;
        if (date == null) {
            dateTime = DateTime.now();
        }

        if (StringUtils.isBlank(format)) {
            format = "yyyy-MM-dd HH:mm:ss.SSS";
        }

        return dateTime.toString(format);
    }

    /**
     * 获取指定日期的周一
     * @param date
     * @return
     */
    public static String getFirstDayOfWeek(Date date) {
        DateTime dateTime = new DateTime(date);

        DateTime firstDayTime = dateTime.withDayOfWeek(1);

        return firstDayTime.toString("YYYY-MM-dd");
    }

    /**
     * 获取指定日期的周日
     * @param date
     * @return
     */
    public static String getLastDayOfWeek(Date date) {
        DateTime dateTime = new DateTime(date);

        DateTime lastDayTime = dateTime.withDayOfWeek(7);

        return lastDayTime.toString("YYYY-MM-dd");
    }

    /**
     * 获取指定日期的所属月份第一天
     * @param date
     * @return
     */
    public static String getFirstDayOfMonth(Date date) {
        DateTime dateTime = new DateTime(date);

        DateTime firstDayTime = dateTime.withDayOfMonth(1);

        return firstDayTime.toString("YYYY-MM-dd");
    }

    /**
     * 获取指定日期的所属月份最后一天
     * @param date
     * @return
     */
    public static String getLastDayOfMoth(Date date) {
        DateTime dateTime = new DateTime(date);

        DateTime lastDayTime = dateTime.dayOfMonth().withMaximumValue();

        return lastDayTime.toString("YYYY-MM-dd");
    }

    /**
     * 获取VIP结束时间
     * @param date
     * @param vipLev
     * @return
     */
    public static Date getVipEndTime(Date date, int vipLev) {

        DateTime startTime = new DateTime(date);

        DateTime endTime = new DateTime();

        if (1 == vipLev) {

            endTime = startTime.plusMonths(6);

        } else if (2 == vipLev) {

            endTime = startTime.plusYears(1);

        } else if (3 == vipLev) {

            endTime = startTime.plusYears(3);

        }

        return endTime.toDate();
    }

    /**
     * 获取VIP有效期的天数
     * @param date
     * @return
     */
    public static int getVipValidDays(Date date) {

        DateTime startTime = DateTime.now();
        DateTime endTime = new DateTime(date);

        Period period = new Period(startTime, endTime, PeriodType.days());

        return period.getDays();
    }

    /**
     * 计算指定时间与当前时间的时间差
     * @param timeUnit 1-秒钟 2-分钟 3-小时
     * @param time
     * @return
     */
    public static int getTimeDiff(int timeUnit, String time) {

        DateTime spcialTime = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime(time);

        DateTime nowTime = DateTime.now();

        if (timeUnit == 1) {

            Period period = new Period(spcialTime, nowTime, PeriodType.seconds());

            return period.getSeconds();

        } else if (timeUnit == 2) {

            Period period = new Period(spcialTime, nowTime, PeriodType.minutes());

            return period.getMinutes();

        } else if (timeUnit == 3) {

            Period period = new Period(spcialTime, nowTime, PeriodType.hours());

            return period.getHours();

        } else if (timeUnit == 4) {

            Period period = new Period(spcialTime, nowTime, PeriodType.days());

            return period.getDays();

        }

        return 0;
    }

    /**
     * 计算指定时间与当前时间的时间差
     * @param timeUnit 1-秒钟 2-分钟 3-小时
     * @param time
     * @return
     */
    public static int getTimeDiff(int timeUnit, Date time) {

        DateTime spcialTime = new DateTime(time);

        DateTime nowTime = DateTime.now();

        if (timeUnit == 1) {

            Period period = new Period(spcialTime, nowTime, PeriodType.seconds());

            return period.getSeconds();

        } else if (timeUnit == 2) {

            Period period = new Period(spcialTime, nowTime, PeriodType.minutes());

            return period.getMinutes();

        } else if (timeUnit == 3) {

            Period period = new Period(spcialTime, nowTime, PeriodType.hours());

            return period.getHours();

        }

        return 0;
    }

    public static void main(String[] args) {
//        DateTime dateTime = DateTime.now();
//        DateTime firstDayTime = dateTime.withDayOfMonth(1);
//        DateTime lastDayTime = dateTime.dayOfMonth().withMaximumValue();
//        System.out.println(firstDayTime.toString("YYYY-MM-dd"));
//        System.out.println(lastDayTime.toString("YYYY-MM-dd"));

//        System.out.println(getVipValidDays(new DateTime(2020, 2, 1, 11, 11, 11, 11).toDate()));
//        System.out.println(getTimeDiff(3, "2019-04-25 15:04:15"));

        DateTime spcialTime = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime("2019-04-25 14:54:15");

        DateTime nowTime = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime("2019-04-26 15:00:00");
        Period period = new Period(spcialTime, nowTime, PeriodType.hours());

        System.out.println(period.getHours());
    }
}
